
/******************************************************************************/
/** \file main.c
 **   MCU : LQFP48
 ******************************************************************************/

#include "bsp_flash.h"
#include "ddl.h"
#include "bsp_uart.h"
#include "bsp_can.h"
#include "bsp_wdt.h"
#include "bsp_timer.h"
#include "bsp_led.h"
#include "bsp_pwm.h"
#include "bsp_adc.h"
#include "bsp_flash.h"
#include "bsp_key.h"
#include "app_key.h"

//内部PLL时钟相关配置
void App_ClkCfg(void)
{
    stc_sysctrl_clk_cfg_t stcCfg;
    stc_sysctrl_pll_cfg_t stcPLLCfg;    
    
	DDL_ZERO_STRUCT(stcPLLCfg);  
    ///< 开启FLASH外设时钟
    Sysctrl_SetPeripheralGate(SysctrlPeripheralFlash, TRUE);
    
    ///< 因将要倍频的PLL作为系统时钟HCLK会达到48MHz：所以此处预先设置FLASH 读等待周期为1 cycle(默认值为0 cycle)
    Flash_WaitCycle(FlashWaitCycle1);
    
    ///< 时钟初始化前，优先设置要使用的时钟源：此处配置PLL
    Sysctrl_SetRCHTrim(SysctrlRchFreq4MHz);             //PLL使用RCH作为时钟源，因此需要先设置RCH    
    
    stcPLLCfg.enInFreq    = SysctrlPllInFreq4_6MHz;     //RCH 4MHz
    stcPLLCfg.enOutFreq   = SysctrlPllOutFreq36_48MHz;  //PLL 输出48MHz
    stcPLLCfg.enPllClkSrc = SysctrlPllRch;              //输入时钟源选择RCH
    stcPLLCfg.enPllMul    = SysctrlPllMul12;            //4MHz x 12 = 48MHz
    Sysctrl_SetPLLFreq(&stcPLLCfg);       
    
    ///< 选择PLL作为HCLK时钟源;
    stcCfg.enClkSrc  = SysctrlClkPLL;
    ///< HCLK SYSCLK
    stcCfg.enHClkDiv = SysctrlHclkDiv1;
    ///< PCLK 为HCLK
    stcCfg.enPClkDiv = SysctrlPclkDiv1;
    ///< 系统时钟初始化
    Sysctrl_ClkInit(&stcCfg);     
}


void Board_GPIO_Init(void)
{	
	stc_gpio_cfg_t stcGpioCfg;
    
    ///< 打开GPIO外设时钟门控
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); 
    
    ///< 端口方向配置->输出 
    stcGpioCfg.enDir = GpioDirOut;	
	stcGpioCfg.enCtrlMode = GpioAHB;
	
	  ///< 端口上下拉配置-> pulldown
    stcGpioCfg.enPu = GpioPuDisable;
    stcGpioCfg.enPd = GpioPdEnable;
	stcGpioCfg.enOD = GpioOdDisable;
    Gpio_Init(GpioPortA, GpioPin5, &stcGpioCfg);
    Gpio_ClrIO(GpioPortA, GpioPin5);  /* 滑动变阻器 - 端*/

	 ///< 端口上下拉配置-> 上拉
    stcGpioCfg.enPu = GpioPuEnable;
    stcGpioCfg.enPd = GpioPdDisable;
	stcGpioCfg.enOD = GpioOdDisable;
    Gpio_Init(GpioPortA, GpioPin6, &stcGpioCfg); /* 滑动变阻器+端*/
	Gpio_SetIO(GpioPortA, GpioPin6);  
		
	// 脉冲模式,需要主脉冲(4s周期的)外,还需要两路相位固定的周期4ms的脉冲	
	// PB14 -- OUT_MCU_27PIN
	stcGpioCfg.enPu = GpioPuEnable;
    stcGpioCfg.enPd = GpioPdDisable;
	stcGpioCfg.enOD = GpioOdDisable;
    Gpio_Init(GpioPortB, GpioPin14, &stcGpioCfg); /* 27pin : 负载放电*/
	Gpio_ClrIO(GpioPortB, GpioPin14);  
			
	stcGpioCfg.enPu = GpioPuEnable;
    stcGpioCfg.enPd = GpioPdDisable;
	stcGpioCfg.enOD = GpioOdDisable;
    Gpio_Init(GpioPortA, GpioPin8, &stcGpioCfg); /* 29pin 5V pulse */
	Gpio_ClrIO(GpioPortA, GpioPin8);  
	
}


int32_t main(void)
{		
	App_ClkCfg(); //如果不初始化,默认使用内部HRC 4Mhz
	 
	Board_GPIO_Init();
	
	BSP_Key_Init();
	
	BSP_Led_Init();
		
	BSP_Uart0_Config(); 			
	printf("\r\nSystem HCLK clock = %d", Sysctrl_GetHClkFreq());
	printf("\r\nSystem PCLK clock = %d", Sysctrl_GetPClkFreq());

	BSP_Timer1_init(1500); // 2ms for pulse used
	
	BSP_Timer3_init(3750); //64分频,周期 = 3750*(1/48M)*64 = 5000us = 5ms 
	
	BSP_wdt_Config();
		
	BSP_Can_Init(); 

	BSP_ADC_Init();
	
	BSP_InnerFlash_Init();

	App_Struct_data_init();
		
//  默认为脉冲模式,先不配置pwm
    PWMPin_reinit_NormalPin();   
//	BSP_PWM_Config();	
//	BSP_PWM_SetDuty(0); 
	
    while(1)	
    {     
		Slide_resistor_detect();		
		CAN_BUS_Process();		
		App_key_process();
		Load_Current_detect();
		Over_Voltage_detect();
		PWM_Output1();			
		Wdt_Feed();		
    }
}




